package com.qitmiaojie.controller.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;


@Slf4j
@RestControllerAdvice
public class GlobalException {
    @ExceptionHandler(value = NoHandlerFoundException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public Result  IllegalArgumentException(NoHandlerFoundException e){
        System.out.println("异常"+e);
        Result result=new Result();
        result.setMsg("错误");
        return result;
    }
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    @ExceptionHandler(Exception.class)
    public Result doOtherException(Exception ex){
        //记录日志
        log.info(ex.getMessage());
        //发送消息给运维
        //发送邮件给开发人员,ex对象发送给开发人员
        ex.printStackTrace();
        return new Result(500,"系统错误，请稍后再试！",null);
    }
    @ExceptionHandler(value = AccessDeniedException.class)
    public Result  AccessDeniedException(AccessDeniedException e){
        System.out.println("异常"+e);
        Result result=new Result();
        result.setStateCode(404);
        result.setMsg("权限不足！请联系管理员");
        return result;
    }
}
